package servlet.classes;

import com.alibaba.fastjson.JSONObject;
import utils.JdbcUtil;
import utils.ResultData;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 *      类别信息修改
 */
@WebServlet(urlPatterns = "/classUpdate")
public class ClassUpdate extends HttpServlet {
    JdbcUtil jdbc = new JdbcUtil();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Integer id = Integer.valueOf(req.getParameter("id"));
        String name = req.getParameter("name");
        String remark = req.getParameter("remark");

        Connection connection = jdbc.getConnection();
        ResultData resultData = null;
        String sql = "update classes_table set classesname=?,remark=? where id=?";

        try {
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1,name);
            preparedStatement.setString(2,remark);
            preparedStatement.setInt(3,id);
            int i = preparedStatement.executeUpdate();

            if (i>0){
                resultData = ResultData.success();
            }else {
                resultData = ResultData.fail();
            }

            //将信息回传到网页
            Object o = JSONObject.toJSON(resultData);
            resp.getWriter().print(o.toString());

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

    }
}
